package com.ait.commonutils.exceptionHandler;


import com.ait.commonutils.Msg;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;

@ControllerAdvice
public class GlobalExceptionHandler {

    //全局异常

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Msg error(Exception e){
        e.printStackTrace();
        if (e instanceof ServletException) return  Msg.fail().code(404).message("请求异常");
        return Msg.fail().code(500).message("服务器异常");
    }

    //自定义异常

    @ExceptionHandler(GuilException.class)
    @ResponseBody
    public Msg error(GuilException e){
        e.printStackTrace();
        int v =201;
        return Msg.fail().code(v).message(e.getMsg());
    }

}
